﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enterprise.Framework.DomainModel;
using Enterprise.Framework.DomainModel.Repositories;
using Enterprise.Framework.Core.Exceptions;
using Enterprise.Framework.DomainModel.Specifications;
using Resto.CostControl.Core.Domain.Resource;
using Resto.CostControl.Common.Utility;

namespace Resto.CostControl.Core.Domain.Model
{
    /// <summary>
    /// 项目点每日收入
    /// </summary>
    public partial class SiteIncome : IAggregationRoot
    {
        #region 构造函数

        internal SiteIncome()
        {
            this.ID = IDHelper.CreateNewID();
        }

        #endregion

        #region 私有属性

        private IEntityRepository<SiteIncome> _Repository;
        private IEntityRepository<SiteIncome> Repository
        {
            get
            {
                if (_Repository == null)
                    _Repository = RestoCostDomainSession.CurrentSession.CreateEntityRepository<SiteIncome>();

                return _Repository;
            }
        }

        #endregion

        #region 业务方法

        internal void Add()
        {
            Validate();

            this.CreateAudit = new Audit();
            this.LastUpdateAudit = new Audit();

            this.Repository.Add(this);
        }

        internal void Update()
        {
            Validate();

            this.LastUpdateAudit = new Audit();

            this.Repository.Update(this);
        }

        internal void Delete()
        {
            this.Repository.Delete(this);
        }

        internal void AddSalesMethod(SalesMethod item)
        {
            bool result = this.SalesMethods.Where(p => p.Type == item.Type).Count() > 0;
            if (result)
                throw new BusinessValidationException(SiteResource.ValidateException_MethodType_Repeat);

            this.SalesMethods.Add(item);
        }

        internal void RemoveSalesMethod(string itemID)
        {
            SalesMethod item = this.SalesMethods.Where(p => p.ID == itemID).FirstOrDefault();
            if (item != null)
            {
                this.SalesMethods.Remove(item);
                item.Delete();
            }
        }

        internal SalesMethod GetSalesMethod(string itemID)
        {
            return this.SalesMethods.Where(p => p.ID == itemID).FirstOrDefault();
        }

        internal void AddSaleStatus(SaleStatus item)
        {
            bool result = this.SaleStatuses.Where(p => 
                            (p.DishID != null && p.DishID == item.DishID)
                            ||
                            (p.DishName == item.DishName)
                            ).Count() > 0;
            if (result)
                throw new BusinessValidationException(SiteResource.ValidateException_MethodType_Repeat);

            this.SaleStatuses.Add(item);
        }

        internal void RemoveSaleStatus(string itemID)
        {
            SaleStatus item = this.SaleStatuses.Where(p => p.ID == itemID).FirstOrDefault();
            if (item != null)
            {
                this.SaleStatuses.Remove(item);
                item.Delete();
            }
        }

        internal SaleStatus GetSaleStatus(string itemID)
        {
            return this.SaleStatuses.Where(p => p.ID == itemID).FirstOrDefault();
        }

        #endregion

        #region 验证方法

        protected void Validate()
        {
            List<string> messageList = new List<string>();

            ValidateDateRepeat(messageList);

            if (messageList.Count > 0)
                throw new BusinessValidationException(messageList.ToArray());
        }


        private void ValidateDateRepeat(IList<string> messageList)
        {
            ISpecification<SiteIncome> spec = Specification<SiteIncome>.Create(p => p.Date == this.Date);
            spec = spec.And(Specification<SiteIncome>.Create(p => p.ID != ID));

            bool result = Repository.Count(spec) > 0;
            if (result)
                messageList.Add(SiteResource.ValidateException_IncomeDate_Repeat);
        }

        #endregion
    }
}
